package controllers
{
	import errors.UIError;
	
	import flash.events.EventDispatcher;
	
	import models.Connection;
	import models.RESTResponse;
	import models.User;
	
	public class UserManager extends EventDispatcher
	{
		private var _current:User;
		
		public function UserManager() {
			_current = new User("Cameron", "toto", "cameron@mail.com");
			_current.oid = 1;
		}
		
		public function connectUser(email:String, password:String) : void {
			if(email.length && password.length) {
				var connection:Connection = new Connection(email, password);
				
				HTTPController.POST('/connect', connection.toXML(), function (response:RESTResponse) : void {
					if(response.success()) {
						var session:XML = response.dataNode();
						HTTPController.sid = session.@oid; 
						_current = new User();
						_current.setXML(session.user[0]);
						WindowManager.open(WindowManager.PROJECT_LIST);
						// Début attente push
						HTTPController.wait();
					} else if(response.error()) {
						UIErrorController.handleRESTErrorCode(response.errorCode());						
					}
				});
				
			} else {
				throw new UIError("user_missing_fields");
			}
		}
		
		public function createUser(name:String, password:String, password2:String, email:String) : void {
			if(name.length && password.length && email.length && password == password2) {
				var user:User = new User(name, password, email);
				
				HTTPController.POST('/users', user.toXML(), function (response:RESTResponse) : void {
					if(response.success()) {
						WindowManager.open(WindowManager.USER_LOGIN);
					} else if(response.error()) {
						UIErrorController.handleRESTErrorCode(response.errorCode());						
					}
				});
			} else {
				if(password != password2) {
					throw new UIError("user_unmatching_passwords");
				}
				throw new UIError("user_missing_fields");
			}
		}
		
		public function updateUser(user:User, name:String, password:String, password2:String, email:String) : void {
			if(password.length == 0 || password == password2) {
				if(name.length && email.length) {
					var oldName:String = user.name;
					user.name = name;
					user.email = email;
					user.password = password;
					HTTPController.PUT('/users/user/' + oldName, user.toXML(), function (response:RESTResponse) : void {
						if(response.success()) {
							_current.setXML(response.dataNode());
						} else {
							UIErrorController.handleRESTErrorCode(response.errorCode());	
						}	
					});
				} else {
					throw new UIError("user_missing_fields");
				}
			} else {
				throw new UIError("user_unmatching_passwords");
			}
		}
		
		public function getUser(oid:String, callback:Function) : void {
			HTTPController.GET('/users/user/' + oid, null, function (response:RESTResponse) : void {
				if(response.success()) {
					var user:User = new User();
					user.setXML(response.dataNode());
					callback(user); 
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});
		}
		
		public function getUserByNameOrEmail(input:String, callback:Function) : void {
			HTTPController.GET('/users/user/search/' + input, null, function (response:RESTResponse) : void {
				if(response.success()) {
					var user:User = new User();
					user.setXML(response.dataNode());
					callback(user);
				} else if(response.warning()) {
					callback(null);
				} else {
					UIErrorController.handleRESTErrorCode(response.errorCode());
				}
			});
		}
		
		public function get currentUser() : User {
			return _current;
		}
	}
}